package cn.ycc1.boot3weixin.controller;

import cn.ycc1.boot3weixin.WeixinConfig;
import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ycc
 * @date 2023/11/12
 */
@RestController
public class WechatPayController {

    @Autowired
    WeixinConfig weixinConfig;
    @Autowired
    WxMpService wxMpService;

    @GetMapping("/pay/mp-auth")
    public String mpAuth() {
        String code = "";
        try {
            // WxMpOAuth2AccessToken accessToken = wxMpService.getOAuth2Service().getAccessToken(CODE);
            WxOAuth2AccessToken wxMpOAuth2AccessToken = wxMpService.getOAuth2Service().getAccessToken(weixinConfig.getAppId(), weixinConfig.getSecret(), code);
            String openid = wxMpOAuth2AccessToken.getOpenId();
            System.out.println("OpenID: " + openid);
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
        return "";
    }
}
